package com.order.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.order.bean.Order;
import com.order.bean.Tables;
import com.order.common.entiry.ResultCode;
import com.order.common.entiry.StateEnum;
import com.order.common.exception.CommonException;
import com.order.common.util.SqlSessionFactoryUtil;
import com.order.mapper.OrderMapper;
import com.order.mapper.TablesMapper;
import com.order.service.TablesService;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.Date;
import java.util.List;
import java.util.UUID;

public class TablesServiceImpl implements TablesService {

    private TablesMapper tablesMapper;

    private OrderMapper orderMapper;

    private SqlSession sqlSession;

    private void closeMapper() {
        if (sqlSession != null) {
            sqlSession.close();
        }
    }

    private void initMapper() {
        SqlSessionFactory factory = SqlSessionFactoryUtil.getInstace();
        SqlSession sqlSession = factory.openSession(true);
        tablesMapper = sqlSession.getMapper(TablesMapper.class);
        orderMapper = sqlSession.getMapper(OrderMapper.class);
    }

    @Override
    public boolean addTable(Tables tables) {
        initMapper();
        boolean flag = tablesMapper.insert(tables);
        closeMapper();
        return flag;
    }

    @Override
    public boolean deleteTableById(Integer id) {
        initMapper();
        boolean flag = tablesMapper.deleteById(id);
        closeMapper();
        return flag;
    }

    @Override
    public boolean deleteBatchTables(Integer[] ids) {
        for (Integer id :ids){
            boolean b = this.deleteTableById(id);
            if (!b) return b;
        }
        return true;
    }

    @Override
    public boolean updateTable(Tables tables) {
        initMapper();
        if ("是".equals(tables.getFree())) {
            tables.setUserId(null);//清除占用的顾客信息
        }
        boolean flag = tablesMapper.update(tables);
        return flag;
    }

    @Override
    public Tables selectTableById(Integer id) {
        initMapper();
        Tables tables = tablesMapper.selectByUserId(id);
        closeMapper();
        return tables;
    }

    @Override
    public List<Tables> selectAllTables(String name) {
        initMapper();
        List<Tables> tables = tablesMapper.selectAll(name);
        closeMapper();
        return tables;
    }

    @Override
    public PageInfo<Tables> selectTablesByPage(String name, Integer pageNum, Integer pageSize) {
        initMapper();
        PageHelper.startPage(pageNum, pageSize);
        List<Tables> tables = this.selectAllTables(name);
        closeMapper();
        return PageInfo.of(tables);
    }

    @Override
    public Order addOrder(Tables tables) {
        Tables dbtables = selectTableByUserId(tables.getUserId());
        if(dbtables != null && !dbtables.getId().equals(tables.getId())){
            throw new CommonException(ResultCode.ALREADY_BOOKED_OTHER_TABLE);
        }
        tables.setFree("否");
        this.updateTable(tables);
        // 生成新的订单
        Order order = new Order();
        // 为订单生成一个唯一的UUID
        order.setId(UUID.randomUUID().toString());
        // 设置订单对应的餐桌ID
        order.setTableId(tables.getId());
        // 设置订单创建的时间
        order.setCreateTime(new Date());
        // 设置订单所属的用户ID
        order.setUserId(tables.getUserId());
        // 设置订单的初始状态
        order.setState(StateEnum.STATE_ENUM_0.getValue());

        orderMapper.insert(order);
        return order;
    }

    @Override
    public Tables selectTableByUserId(Integer userId) {
        initMapper();
        Tables tables = tablesMapper.selectByUserId(userId);
        closeMapper();
        return tables;
    }

    @Override
    public void removeOreder(Tables tables) {
        initMapper();
        tablesMapper.removeOrder(tables.getId());
        if(null != tables.getUserId() && !tables.getUserId().equals("")){
            Order order = new Order();
            order.setUserId(tables.getUserId());
            order.setState(StateEnum.STATE_ENUM_1.getValue());
            orderMapper.updateUserOrderState(order);
        }
    }
}
